package com.ymatou.iapi.couponservice.testcase;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.couponservice.parameter.CouponInfo;
import com.ymatou.iapi.couponservice.parameter.CouponInfos;
import com.ymatou.iapi.couponservice.parameter.GetCouponListByCodeBean;
import com.ymatou.iapi.couponservice.service.GetCouponListByCodeCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponWapper;
import com.ymttest.utils.YMTDateUtil;

@TestSuite
public class Ts_GetCouponListByCode {
	private static GetCouponListByCodeBean getcouponlistbycodeBean;
    private static GetCouponListByCodeCall getcouponlistbycodeCall;

    @BeforeClass
    public static void setUp() {
        Logger.createResultFile("根据优惠券编号获取优惠券信息用例");
    }

    @Before
    public void caseUp() {
        getcouponlistbycodeBean = new GetCouponListByCodeBean();
        getcouponlistbycodeCall = new GetCouponListByCodeCall();
    }

    @AfterClass
    public static void tearDown() {
        Logger.generateResult(getcouponlistbycodeCall.getOpurl());
    }

    @After
    public void caseDown() {
        Logger.end();
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetCouponListByCode_001() {
        Logger.start(true, "根据优惠券编号获取优惠券信息-平台短码优惠券");
        try {
            HashMap<String, Map> couponInfoMap = new HashMap<String, Map>();
            List<String> couponCodesList = new ArrayList<String>();
//               YmtCouponBatchWapper ymtCouponBatchWapper = new YmtCouponBatchWapper();
//
//            // 平台短码优惠券
//            List<Map> couponInfo1 = ymtCouponBatchWapper.getTop1PlatformShortCodeCouponInfo();
//            String couponCode1 = couponInfo1.get(0).get("sCouponCode").toString();
            couponCodesList.add("170222824146EE3WJOT1");
//            couponInfoMap.put(couponCode1, couponInfo1.get(0));

            getcouponlistbycodeBean.setCouponCodes(couponCodesList);
            getcouponlistbycodeCall.setData(getcouponlistbycodeBean);
            getcouponlistbycodeCall.callService();

            verifyReturn(couponInfoMap);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetCouponListByCode_002() {
        Logger.start(true, "根据优惠券编号获取优惠券信息-平台链接优惠券");
        try {
            HashMap<String, Map> couponInfoMap = new HashMap<String, Map>();
            List<String> couponCodesList = new ArrayList<String>();
            YmtCouponBatchWapper ymtCouponBatchWapper = new YmtCouponBatchWapper();

            // 平台链接优惠券
            List<Map> couponInfo2 = ymtCouponBatchWapper.getTop1PlatformLinkCouponInfo();
            String couponCode2 = couponInfo2.get(0).get("sCouponCode").toString();
            couponCodesList.add(couponCode2);
            couponInfoMap.put(couponCode2, couponInfo2.get(0));

            getcouponlistbycodeBean.setCouponCodes(couponCodesList);
            getcouponlistbycodeCall.setData(getcouponlistbycodeBean);
            getcouponlistbycodeCall.callService();

            verifyReturn(couponInfoMap);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetCouponListByCode_003() {
        Logger.start(true, "根据优惠券编号获取优惠券信息-商家全场优惠券");
        try {
            HashMap<String, Map> couponInfoMap = new HashMap<String, Map>();
            List<String> couponCodesList = new ArrayList<String>();
            YmtCouponBatchWapper ymtCouponBatchWapper = new YmtCouponBatchWapper();

            // 商家全场优惠券
            List<Map> couponInfo3 = ymtCouponBatchWapper.getTop1SellerCouponInfo();
            couponCodesList.add(couponInfo3.get(0).get("sCouponCode").toString());
            String couponCode3 = couponInfo3.get(0).get("sCouponCode").toString();
            couponCodesList.add(couponCode3);
            couponInfoMap.put(couponCode3, couponInfo3.get(0));

            getcouponlistbycodeBean.setCouponCodes(couponCodesList);
            getcouponlistbycodeCall.setData(getcouponlistbycodeBean);
            getcouponlistbycodeCall.callService();

            verifyReturn(couponInfoMap);
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    @Ignore
    // 商家券不能指定商品
    public void Tc_GetCouponListByCode_004() {
        Logger.start(true, "根据优惠券编号获取优惠券信息-商家商品优惠券");
        try {
            HashMap<String, Map> couponInfoMap = new HashMap<String, Map>();
            List<String> couponCodesList = new ArrayList<String>();
            YmtCouponBatchWapper ymtCouponBatchWapper = new YmtCouponBatchWapper();

            // 商家商品优惠券
            List<Map> couponInfo4 = ymtCouponBatchWapper.getTop1SellerProductCouponInfo();
            couponCodesList.add(couponInfo4.get(0).get("sCouponCode").toString());
            String couponCode4 = couponInfo4.get(0).get("sCouponCode").toString();
            couponCodesList.add(couponCode4);
            couponInfoMap.put(couponCode4, couponInfo4.get(0));

            getcouponlistbycodeBean.setCouponCodes(couponCodesList);
            getcouponlistbycodeCall.setData(getcouponlistbycodeBean);
            getcouponlistbycodeCall.callService();

            verifyReturn(couponInfoMap);
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    @Ignore
    public void Tc_GetCouponListByCode_005() {
        Logger.start(true, "根据优惠券编号获取优惠券信息-混合优惠券");
        try {
            HashMap<String, Map> couponInfoMap = new HashMap<String, Map>();
            List<String> couponCodesList = new ArrayList<String>();
            YmtCouponBatchWapper ymtCouponBatchWapper = new YmtCouponBatchWapper();

            // 平台短码优惠券
            List<Map> couponInfo1 = ymtCouponBatchWapper.getTop1PlatformShortCodeCouponInfo();
            String couponCode1 = couponInfo1.get(0).get("sCouponCode").toString();
            couponCodesList.add(couponCode1);
            couponInfoMap.put(couponCode1, couponInfo1.get(0));

            // 平台链接优惠券
            List<Map> couponInfo2 = ymtCouponBatchWapper.getTop1PlatformLinkCouponInfo();
            String couponCode2 = couponInfo2.get(0).get("sCouponCode").toString();
            couponCodesList.add(couponCode2);
            couponInfoMap.put(couponCode2, couponInfo2.get(0));

            // 商家全场优惠券
            List<Map> couponInfo3 = ymtCouponBatchWapper.getTop1SellerCouponInfo();
            couponCodesList.add(couponInfo3.get(0).get("sCouponCode").toString());
            String couponCode3 = couponInfo3.get(0).get("sCouponCode").toString();
            couponCodesList.add(couponCode3);
            couponInfoMap.put(couponCode3, couponInfo3.get(0));

            // 商家商品优惠券
            List<Map> couponInfo4 = ymtCouponBatchWapper.getTop1SellerProductCouponInfo();
            couponCodesList.add(couponInfo4.get(0).get("sCouponCode").toString());
            String couponCode4 = couponInfo4.get(0).get("sCouponCode").toString();
            couponCodesList.add(couponCode4);
            couponInfoMap.put(couponCode4, couponInfo4.get(0));

            getcouponlistbycodeBean.setCouponCodes(couponCodesList);
            getcouponlistbycodeCall.setData(getcouponlistbycodeBean);
            getcouponlistbycodeCall.callService();

            verifyReturn(couponInfoMap);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    private void verifyReturn(HashMap<String, Map> couponInfoMap) throws ParseException
    {
        Logger.verifyEquals("0", getcouponlistbycodeCall.getStatusCode1(), "验证返回Code");
        Logger.verifyEquals("操作成功！",
                getcouponlistbycodeCall.getMessage(), "验证返回Message");

        CouponInfos couponInfos = getcouponlistbycodeCall.getCouponInfos();

        for (CouponInfo couponInfo : couponInfos.getCouponInfos()) {

            Logger.comment("验证CouponCode为" + couponInfo.getCouponCode() + "记录详细信息");
            Iterator iter = couponInfoMap.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                Object key = entry.getKey();

                if (key.toString().equals(couponInfo.getCouponCode()))
                {
                    Map val = (Map) entry.getValue();

                    Map result = new YmtCouponWapper().selectCouponInfoByCouponCode(couponInfo.getCouponCode()).get(0);
                	Logger.verifyEquals(Integer.parseInt(result.get("iScenarioType").toString()), couponInfo.getProductLimitType(),
            				"验证返回字段productLimitType与Ymt_CouponSetting表中iScenarioType一致");
                	
                    if (val.get("sCouponName") != null && !val.get("sCouponName").equals(""))
                    {
                        Logger.verifyEquals(val.get("sCouponName").toString(), couponInfo.getCouponName(),
                                "验证返回CouponName与数据库表Ymt_CouponBatch中sCouponName一致");
                    }
                    else {
                        Logger.verifyIsNull(couponInfo.getCouponName(),
                                "验证返回CouponName是Null");
                    }
                    // 该字段下个scrum加入
                    // if (val.get("CouponDes") != null &&
                    // !val.get("CouponDes").toString().trim().equals(""))
                    // {
                    // Logger.verifyEquals(val.get("CouponDes").toString(),
                    // couponInfo.getCouponDes(),
                    // "验证返回CouponDes与数据库表Ymt_CouponBatch中CouponDes一致");
                    // }
                    // else {
                    // Logger.verifyIsNull(couponInfo.getSellerIds(),
                    // "验证返回CouponDes是Null");
                    // }
                    Logger.verifyEquals(
                            0,
                            couponInfo.getMeetAmount().compareTo(
                                    new BigDecimal(val.get("fMinOrderValue").toString())),
                            "验证返回MeetAmount与Ymt_CouponValue中一致");
                    Logger.verifyEquals(
                            0,
                            couponInfo.getDeductionAmount().compareTo(
                                    new BigDecimal(val.get("fCouponValue").toString())),
                            "验证返回DeductionAmount与Ymt_CouponValue中一致");
                    Logger.verifyEquals(YMTDateUtil.convertDateStrToAnotherFormat(
                            val.get("ValidStart").toString().substring(0, 19), "yyyy-MM-dd HH:mm:ss",
                            "yyyy-MM-dd'T'HH:mm:ss"),
                            couponInfo.getStartTime().substring(0, 19), "验证返回StartTime");
                    Logger.verifyEquals(
                            YMTDateUtil.convertDateStrToAnotherFormat(
                                    val.get("ValidEnd").toString().substring(0, 19).substring(0, 19),
                                    "yyyy-MM-dd HH:mm:ss",
                                    "yyyy-MM-dd'T'HH:mm:ss"), couponInfo.getEndTime().substring(0, 19),
                            "验证返回EndTime");
                    Logger.verifyEquals(false,
                            couponInfo.isIsBestShopping(), "验证返回IsBestShopping");
                    Logger.verifyEquals(Integer.parseInt(val.get("iUserType").toString()),
                            couponInfo.getUserTypeInfo(), "验证返回UserTypeInfo");

                    if (couponInfo.getSellerIds() != null)
                    {
                        Logger.verifyEquals(val.get("sSellerIds").toString(), couponInfo.getSellerIds(),
                                "验证返回SellerIds");
                    }
                    else {
                        Logger.verifyIsNull(val.get("sSellerIds"),
                                "验证返回SellerIds是Null");
                    }

                    if (val.get("sProductCategories") != null)
                    {
                        Logger.verifyEquals(val.get("sProductCategories").toString(),
                                couponInfo.getProductCategories(),
                                "验证返回ProductCategories");
                    }
                    else {
                        Logger.verifyIsNull(couponInfo.getProductCategories(),
                                "验证返回ProductCategories是Null");
                    }

                    if (val.get("sSpecificProducts") != null) {
                        Logger.verifyEquals(val.get("sSpecificProducts").toString(),
                                couponInfo.getSpecificProducts(),
                                "验证返回SpecificProducts");
                    }
                    else {
                        Logger.verifyIsNull(couponInfo.getSpecificProducts(),
                                "验证返回SpecificProducts是Null");
                    }

                    if (val.get("sProductBrands") != null)
                    {
                        Logger.verifyEquals(val.get("sProductBrands").toString(), couponInfo.getProductBrands(),
                                "验证返回ProductBrands");
                    }
                    else {
                        Logger.verifyIsNull(couponInfo.getProductBrands(),
                                "验证返回ProductBrands是Null");
                    }

                    if (val.get("sActivityIds") != null)
                    {
                        Logger.verifyEquals(val.get("sActivityIds").toString(), couponInfo.getActivityIds(),
                                "验证返回ActivityIds");
                    }
                    else {
                        Logger.verifyIsNull(couponInfo.getActivityIds(),
                                "验证返回ActivityIds是Null");
                    }

                    if (val.get("sUsePlatforms") != null)
                    {
                        Logger.verifyEquals(val.get("sUsePlatforms").toString(), couponInfo.getUsePlatforms(),
                                "验证返回UsePlatforms");
                    }
                    else {
                        Logger.verifyIsNull(couponInfo.getUsePlatforms(),
                                "验证返回UsePlatforms是Null");
                    }
                }
                else {
                    break;
                }
            }
        }
    }
}
